package controller  {
import data.RadioEvents;

import flash.events.EventDispatcher;
    import flash.net.NetStream; 
	import flash.net.NetConnection; 
	import flash.events.NetStatusEvent;
	import flash.events.AsyncErrorEvent
	import flash.events.SecurityErrorEvent; 
    import flash.events.Event; 
	import data.RadioEvents;
	
	import flash.media.SoundTransform;
	
  	public class PlayAACStreamService extends EventDispatcher{ 
        private var streamURL:String; //="http://208.43.60.66:8000/bbn?type=.flv"
									//"http://208.43.60.66:8000/cjtl-fm?type=.flv"
									//"http://stream.radiodacha.ru:8000/radiodacha_28?type=.flv";
		private var stream:NetStream; 
        private var connection:NetConnection; 
		public function PlayAACStreamService() { 
			
        }
		
		public function play(streamurl:String):void {
			streamURL = stream;
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
         	connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			connection.connect(null);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void{
			//trace("netStatusHandler1: "+event.info.code)
			if(event.info.code=="NetConnection.Connect.Success"){
				connection.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
         		connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				connectStream();
			}
			if(event.info.code=="NetStream.Buffer.Flush"){
				stream.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
				dispatchEvent(new Event(RadioEvents.STREAMING_PLAY));
			}
        }

		private function asyncErrorHandler(event:AsyncErrorEvent):void{
           // trace("asyncErrorHandler: " + event);
        }
		
		private function securityErrorHandler(event:SecurityErrorEvent):void{
            //trace("securityErrorHandler: " + event);
        }
		
		private function onMetaData(event:Object):void{
            trace("metaDataHandler: " + event);
			var songName:String = "";
			if (event.title) {
				songName = event.title;
			}else {
				songName = event.name;
			}
			trace("Songname= " +songName);
			//Data.player_box.tName.text = songName;
			//dispatchEvent(new StageEvent(data.RadioEvents.));
		}
		
		private function cuePointHandler(event:Object):void{
			trace("cuePointHandler: " + event);
        }
		
		public function set volume (value:Number):void {
			stream.soundTransform = new SoundTransform(value);
		}
		
		private function connectStream():void {
			stream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			stream.bufferTime = 5;
			stream.client = new Object();
            //stream.client.onCuePoint = cuePointHandler;
            stream.client.onMetaData = onMetaData;
			stream.soundTransform = new SoundTransform(.5);	
			stream.play(streamURL);	
		}
		
		public function stop():void {
			stream.pause();
		}
		
		public function destroy():void {
			stream.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			stream = null;
			connection = null;
		}
    } 
} 